package com.xm.dao.reception_desk;

import com.xm.entity.CommodityDetails;
import com.xm.exception.SqlErrorException;
import com.xm.util.BaseUtil;
import org.intellij.lang.annotations.Language;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 商品详情
 * @author 康文程
 * @date 2024-1-23 下午 2:00
 */
public class CommodityDetailsDao extends BaseUtil {
    /**
     * 根据商品ID查询商品详情
     *
     * @param commodityId 商品ID
     * @return CommodityDetails 商品详情
     */
    public CommodityDetails getCommodityDetailsById(Integer commodityId) {
        @Language("SQL")
        String sql = "SELECT details_id, details_images, details_video, commodity_id FROM commodity_details WHERE commodity_id = ?";
        ResultSet rs = null;
        try {
            rs = executeQuery(sql, commodityId);
            if (rs.next()) {
                CommodityDetails commodityDetails = new CommodityDetails();
                commodityDetails.setDetailsId(rs.getInt("details_id"));
                commodityDetails.setDetailsImages(rs.getString("details_images"));
                commodityDetails.setDetailsVideo(rs.getString("details_video"));
                commodityDetails.setCommodityId(rs.getInt("commodity_id"));
                return commodityDetails;
            }
        } catch (SqlErrorException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(rs);
        }

        return null;
    }
}
